export default {
  namespaced: true,
  state: {
    type: 'all',
    list: JSON.parse(localStorage.getItem('zuo')) || [
      {
        id: 100,
        name: "吃饭",
        isDone: true,
      },
      {
        id: 201,
        name: "睡觉",
        isDone: false,
      },
      {
        id: 103,
        name: "打豆豆",
        isDone: true,
      },
    ],
  },
  mutations: {
    //按钮选中
    addFn(state, id) {
      const obj = state.list.find(item => item.id === id)
      obj.isDone = !obj.isDone
    },
    //删除
    clearMover(state, id) {
      state.list = state.list.filter(item => item.id !== id)
    },
    //增加
    inputAddFn(state, name) {
      state.list.unshift({
        id: +new Date(),
        name,
        isDone: false,
      })
    },
    //修改全选
    changeiSall(state, val) {
      console.log(val);
      state.list.forEach(item => item.isDone = val)
    },
    //高亮
    fn(state, val) {
      state.type = val
    },
    //删除
    claer(state, id) {
      state.list = state.list.filter(item => item.isDone !== true)
    }
  },
  getters: {
    //剩余多少
    sheng(state) {
      return state.list.filter(item => item.isDone === false).length
    },
    // 筛选有没有完成按钮
    clarShow(state) {
      return state.list.some(item => item.isDone === true)
    },
    //筛选完成未完成的
    listFn(state) {
      if (state.type === 'active') {
        return state.list.filter(item => item.isDone === false)
      } else if (state.type === 'activeko') {
        return state.list.filter(item => item.isDone === true)
      } else {
        return state.list
      }
    }
  },
  actions: {
    clears(context) {
      setTimeout(() => {
        context.commit('claer')
      }, 1000)
    }
  }

}